/*
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author ebeltran
 */
public class MyConexion {

    private static MyConexion instancia;
    private final String className;
    private final String className1;
    private final String url;
    private final String url1;
    private final String loginDB;
    private final String passwordDB;

    public static MyConexion getDBConexion()
            throws Exception {
        if (instancia == null) {
            instancia = new MyConexion();
        }
        return instancia;
    }

    private MyConexion()
            throws Exception {
        className = "org.firebirdsql.jdbc.FBDriver";
        className1 = "jstels.jdbc.dbf.DBFDriver";
        String strOS = System.getProperty("os.name").toLowerCase();

        if (strOS.startsWith("windows")) {
            url = "jdbc:firebirdsql://localhost:3050/IndiaDB_New";
            url1 = "jdbc:jstels:dbf:C:\\india\\ENTREGAS";
        } else {
            url = "jdbc:firebirdsql://192.168.1.1:3050/IndiaDB_New";
            url1 = "jdbc:jstels:dbf:/home/india/india/ENTREGAS";
        }
        loginDB = "SYSDBA";
        passwordDB = "masterkey";
        File file = new File("config.dat");
        file.createNewFile();
    }

    public Connection getConexion()
            throws Exception {
        try {
            Class.forName(className).newInstance();
        } catch (InstantiationException e) {
            System.out.println("error 1" + e.getMessage());
        } catch (IllegalAccessException e) {
            System.out.println("error 2" + e.getMessage());
        } catch (ClassNotFoundException e) {
            System.out.println("error 3" + e.getMessage());
        }
        try {
            return DriverManager.getConnection(url, loginDB, passwordDB);
        } catch (SQLException e) {
        }
        throw new Exception("Error en el getConnection Firebird..");
    }

    public Connection getConexion1()
            throws Exception {
        try {
            Class.forName(className1).newInstance();
        } catch (InstantiationException e) {
            System.out.println("error 1" + e.getMessage());
        } catch (IllegalAccessException e) {
            System.out.println("error 2" + e.getMessage());
        } catch (ClassNotFoundException e) {
            System.out.println("error 3" + e.getMessage());
        }
        try {
            return DriverManager.getConnection(url1);
        } catch (SQLException e) {
        }
        throw new Exception("Error en el getConnection xBase...");
    }

    public void cerrarConeccion() throws Exception {
        try {
            instancia.finalize();
        } catch (Throwable e) {
            Exception ex = new Exception(e);
            throw ex;
        }
    }
}
